﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CE.iPhone.PList;
namespace cczEdit.DramaDialog
{
    public partial class EditDramaSetStateDialog : Form
    {
        PListDict curDict;
        public EditDramaSetStateDialog()
        {
            InitializeComponent();
            foreach (Hero hero in UCDrama.heros)
            {
                comboBoxHero.Items.Add(hero.GetName());
            }
            foreach (KeyValuePair<int, string> kv in Constant.charStateType)
            {
                comboBoxState.Items.Add(kv.Value);
            }
            foreach (KeyValuePair<int, string> kv in Constant.ownTypeDic)
            {
                comboBoxOwner.Items.Add(kv.Value);
            }


        }
        public void Show(IWin32Window owner, PListDict dict)
        {
            curDict = dict;
            listBoxStates.Items.Clear();
            if (listBoxStates.Items.Count <= 1)
                buttonDelState.Enabled = false;

            PListInteger dataType = (PListInteger)curDict["DataType"];
            PListDict data = (PListDict)curDict["Data"];
            PListInteger bfIndex = (PListInteger)data["BFIndex"];
            PListInteger unitID = (PListInteger)data["UnitID"];
            PListInteger originX = (PListInteger)data["OriginX"];
            PListInteger originY = (PListInteger)data["OriginY"];
            PListInteger dstX = (PListInteger)data["DstX"];
            PListInteger dstY = (PListInteger)data["DstY"];
            PListInteger ownerType = (PListInteger)curDict["Owner"];
            PListInteger hpDelta = (PListInteger)curDict["HPDelta"];
            PListInteger mpDelta = (PListInteger)curDict["MPDelta"];


            PListArray states = (PListArray)curDict["States"];

            comboBoxDataType.SelectedIndex = (int)dataType.Value;
            textBoxBFIndex.Text = bfIndex.Value.ToString();
            textBoxDstX.Text = dstX.Value.ToString();
            textBoxDstY.Text = dstY.Value.ToString();
            textBoxOriginX.Text = originX.Value.ToString();
            textBoxOriginY.Text = originY.Value.ToString();
            textBoxHP.Text = hpDelta.Value.ToString();
            textBoxMP.Text = mpDelta.Value.ToString();
            foreach (PListInteger state in states)
            {
                listBoxStates.Items.Add(Constant.charStateType[(int)state.Value]);
                
            }
            Hero hero = UCDrama.heros.Find((h) => { return h.num == unitID.Value; });
            comboBoxHero.SelectedItem = hero.GetName();
            comboBoxOwner.SelectedIndex = (int)ownerType.Value;

            ShowDialog(owner);
        }
        private void comboBoxDataType_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxBFIndex.Enabled = false;
            groupBox1.Enabled = false;
            comboBoxHero.Enabled = false;
            if (comboBoxDataType.SelectedIndex == 1)
            {
                comboBoxHero.Enabled = true;
            }
            else if (comboBoxDataType.SelectedIndex == 0)
            {
                textBoxBFIndex.Enabled = true;
            }
            else
                groupBox1.Enabled = true;
        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            curDict["DataType"] =new PListInteger(comboBoxDataType.SelectedIndex);
            PListDict data = (PListDict)curDict["Data"];
            if (comboBoxHero.SelectedItem != null)
            {
                Hero hero = UCDrama.heros.Find((h) => { return h.GetName() == comboBoxHero.Text.ToString(); });
                data["UnitID"] = new PListInteger(hero.num);
            }
            data["BFIndex"] = new PListInteger(System.Int32.Parse(textBoxBFIndex.Text));
            data["OriginX"] = new PListInteger(System.Int32.Parse(textBoxOriginX.Text));
            data["OriginY"] = new PListInteger(System.Int32.Parse(textBoxOriginY.Text));
            data["DstX"] = new PListInteger(System.Int32.Parse(textBoxDstX.Text));
            data["DstY"] = new PListInteger(System.Int32.Parse(textBoxDstY.Text));
            curDict["Owner"] = new PListInteger(comboBoxOwner.SelectedIndex);
            curDict["HPDelta"] = new PListInteger(System.Int32.Parse(textBoxHP.Text));
            curDict["MPDelta"] = new PListInteger(System.Int32.Parse(textBoxMP.Text));

        }

        private void buttonAddState_Click(object sender, EventArgs e)
        {
            if (comboBoxState.SelectedIndex == (int)CharStateChange.CharStateChangeNone)
            {
                return;
            }
            PListArray states = (PListArray)curDict["States"];
            foreach (PListInteger elem in states)
            {
                string elemStr = Constant.charStateType[(int)elem.Value];
                if (elemStr == comboBoxState.SelectedItem.ToString())
                {
                    MessageBox.Show("重复的状态");
                    return;
                }
            }

            states.Add(new PListInteger(comboBoxState.SelectedIndex));
            listBoxStates.Items.Add(Constant.charStateType[comboBoxState.SelectedIndex]);
            listBoxStates.SelectedItem = Constant.charStateType[comboBoxState.SelectedIndex];
            if (listBoxStates.Items.Count > 1)
                buttonDelState.Enabled = true;
        }

        private void buttonDelState_Click(object sender, EventArgs e)
        {
            PListArray states = (PListArray)curDict["States"];
            IPListElement elemDel =null;
            foreach(KeyValuePair<int,string> kv in Constant.charStateType){
                if (kv.Value == listBoxStates.SelectedItem.ToString())
                {
                    foreach (PListInteger elem in states)
                    {
                        if (elem.Value == kv.Key)
                            elemDel = elem;
                    }
                }
                   
            }
            states.Remove(elemDel);
            string itemDel = listBoxStates.SelectedItem.ToString();
            listBoxStates.Items.Remove(itemDel);
            listBoxStates.SelectedIndex = 0 ;
            if (listBoxStates.Items.Count <= 1)
                buttonDelState.Enabled = false;
            
        }
    }
}
